package com.burgeon.backend.college.control_panel.controller;

import com.burgeon.backend.SHOPBACKEND.controller.BaseController;
import com.burgeon.backend.college.control_panel.entity.Course;
import com.burgeon.backend.college.control_panel.entity.Exam;
import com.burgeon.backend.college.control_panel.service.BackendCollegeService;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("backend/college")
public class BackendCollegeController extends BaseController {
    @Autowired
    private BackendCollegeService backendCollegeService;
//成绩导出

    @RequestMapping("exportAchievement")
    public ResponseResult<Void> exportAchievement(String examId, HttpServletResponse response) {
        backendCollegeService.exportAchievement(examId, response);
        return null;
    }

    /**
     * 排序
     *
     * @param id  examItem id
     * @param num 1向下 -1 向上 触顶不变
     * @return
     */
    @RequestMapping("courseSort")
    public ResponseResult<Void> CourseSort(String id, Integer num) {
        backendCollegeService.upExamItemSort(id, num);
        return new ResponseResult<>(SUCCESS);
    }


    //修改考试

    /**
     * @param id           考试id
     * @param course_id    课程id
     * @param publisher    发布人
     * @param startTime    开始时间 yyyy-MM-dd HH:mm:ss
     * @param endTime      结束时间
     * @param examDuration 考试限制时间 int 分钟
     * @return
     */
    @RequestMapping("setExam")
    public ResponseResult<Void> setExam(String id, String course_id, String publisher,
                                        String startTime, String endTime, Integer examDuration) {

        backendCollegeService.setExam(id, course_id, publisher, startTime, endTime, examDuration);

        return new ResponseResult<>(SUCCESS);
    }


    //删除考试
    @RequestMapping("delExamById")
    public ResponseResult<Void> delExamById(String id) {
        backendCollegeService.delExam(id);
        return new ResponseResult<>(SUCCESS);
    }

    //删除课程

    @RequestMapping("delCourseById")
    public ResponseResult<Void> delCourseById(String id) {
        backendCollegeService.delCourse(id);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 成绩列表
     *
     * @param name
     * @param examId
     * @param start
     * @return
     */
    @RequestMapping("getExamRecord")
    public ResponseResult<HashMap> getExamRecord(String name, String examId, Integer start) {
        HashMap data = backendCollegeService.getExamRecord(name, examId, start);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }


    //同意复考 考试记录id
    @RequestMapping("consentExam")
    public ResponseResult<Void> consentExam(String id) {
        backendCollegeService.consentExam(id);
        return new ResponseResult<>(SUCCESS);
    }


    /**
     * 考试数据
     *
     * @param id
     * @return
     */
    @RequestMapping("getExamById")
    public ResponseResult<Exam> getExam(String id) {
        Exam data = backendCollegeService.getExam(id);
        return new ResponseResult<Exam>(SUCCESS, data);
    }

    /**
     * 考试开关
     *
     * @param id
     * @param EW
     * @return
     */
    @RequestMapping("upExamSwitch")
    public ResponseResult<Void> upExamSwitch(String id, String EW) {
        backendCollegeService.upExamSwitch(id, EW);
        return new ResponseResult<>(SUCCESS);
    }


    /**
     * 上传考题
     *
     * @param id
     * @param xls 文件
     * @return
     */
    @RequestMapping("uploadingExam")
    public ResponseResult<Void> uploadingExam(String id, MultipartFile xls) {
        backendCollegeService.uploadingExam(id, xls);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 创建考试
     *
     * @param id           课程id
     * @param publisher    发布人
     * @param startTime    开始时间 yyyy-MM-dd HH:mm:ss
     * @param endTime      结束时间
     * @param examDuration 考试时长 分钟
     * @param account
     * @return
     */
    @RequestMapping("createExam")
    public ResponseResult<HashMap> createExam(String id,
                                              String publisher, String startTime, String endTime,
                                              Integer examDuration, String account) {
        HashMap data = backendCollegeService.createExam(id, publisher, startTime, endTime, examDuration, account);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }

    /**
     * 考试 列表
     *
     * @param field   A课程名 B发布人 C无条件
     * @param message 值
     * @param start   20
     * @param account
     * @return
     */
    @RequestMapping("getExam")
    public ResponseResult<HashMap> getExam(String field, String message, Integer start, String account) {
        HashMap data = backendCollegeService.getExam(field, message, start, account);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }

    /**
     * 删除选项
     *
     * @param id
     * @return
     */
    @RequestMapping("delOption")
    public ResponseResult<Void> delOption(String id) {
        backendCollegeService.delOption(id);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 删除考题
     *
     * @param id
     * @return
     */
    @RequestMapping("delExamItem")
    public ResponseResult<Void> delExamItem(String id) {
        backendCollegeService.delExamItem(id);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 修改选项
     *
     * @param id    选项id
     * @param value 选项
     * @return
     */
    @RequestMapping("upOption")
    public ResponseResult<Void> upOption(String id, String value) {
        backendCollegeService.upOption(id, value);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 修改考题
     *
     * @param id     考题id
     * @param title  题目
     * @param score  分数
     * @param result 答案
     * @return
     */
    @RequestMapping("upExamItem")
    public ResponseResult<Void> upExamItem(String id, String title, Integer score, String result) {
        backendCollegeService.upExamItem(id, title, score, result);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 创建选项
     *
     * @param eiid  考题id
     * @param value 选项
     * @return
     */
    @RequestMapping("createOption")
    public ResponseResult<Void> createOption(String eiid, String value) {
        backendCollegeService.createOption(eiid, value);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 创建考题
     *
     * @param eid    考试id
     * @param title  题目
     * @param score  分数
     * @param result 答案
     * @return
     */
    @RequestMapping("createExamItem")
    public ResponseResult<Void> createExamItem(String eid, String title, Integer score,
                                               String result) {
        backendCollegeService.createExamItem(eid, title, score, result);
        return new ResponseResult<>(SUCCESS);
    }


    /**
     * 下载模板
     *
     * @param response
     * @return
     */
    @RequestMapping("downloadTemplate")
    public ResponseResult<Void> downloadTemplate(HttpServletResponse response) {
        backendCollegeService.downloadTemplate(response);
        //return new ResponseResult<>(SUCCESS);
        return null;
    }

    /**
     * 发布考题
     *
     * @param id 考题id
     * @return
     */
    @RequestMapping("publishExam")
    public ResponseResult<Void> publishExam(String id) throws Exception {
        backendCollegeService.publishExam(id);

        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 课程列表 下拉选项用
     *
     * @param account
     * @return
     */
    @RequestMapping("getCourseList")
    public ResponseResult<List<Course>> getCourseList(String account) {
        List<Course> data = backendCollegeService.getCourseList(account);
        return new ResponseResult<List<Course>>(SUCCESS, data);
    }


    /**
     * 课程列表
     *
     * @param field   A课程名称 B发布人 C无条件
     * @param message
     * @param start
     * @param account
     * @return
     */
    @RequestMapping("getCourse")
    public ResponseResult<HashMap> getCourse(String field, String message, Integer start, String account) {
        HashMap data = backendCollegeService.getCourse(field, message, account, start);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }


    /**
     * 创建课程
     *
     * @param imgFile   封面图片
     * @param account
     * @param introduce 课程介绍
     * @param study     学习时间 分钟
     * @param isShow    是否显示 YN
     * @param startTime 开始时间 yyyy-MM-dd HH:mm:ss
     * @param endTime   结束时间 yyyy-MM-dd HH:mm:ss
     * @param publisher 发布人
     * @param title     标题
     * @return
     */
    @RequestMapping("createCourse")
    public ResponseResult<Void> createCourse(MultipartFile imgFile, String code, String type, String account, String introduce,
                                             Integer study, String isShow, String startTime, String endTime,
                                             String publisher, String title) throws Exception {
        Course course = new Course();

        course.setToken(account);
        course.setIntroduce(introduce);
        course.setStudy(study);
        course.setIsShow(isShow);
        course.setStartTime(startTime);
        course.setEndTime(endTime);
        course.setPublisher(publisher);
        course.setTitle(title);
        backendCollegeService.createCourse(imgFile, code, type, course);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 上传视频/pdf
     *
     * @param file    课件文件
     *                //  * @param type 课件类型 P:pdf V:视频
     * @param account
     * @return
     */
    @RequestMapping("uploadCourseware")
    public ResponseResult<HashMap> uploadCourseware(MultipartFile file, String account) {
        HashMap data = backendCollegeService.uploadCourseware(file, account);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }


    /**
     * 修改课程
     *
     * @param imgFile   封面图片
     * @param id        课程id
     * @param account
     * @param introduce 课程介绍
     * @param study     学习时间 分钟
     * @param isShow    是否显示 YN
     * @param startTime 开始时间 yyyy-MM-dd HH:mm:ss
     * @param endTime   结束时间 yyyy-MM-dd HH:mm:ss
     * @param publisher 发布人
     * @param title     标题
     * @return
     */
    @RequestMapping("upCourse")
    public ResponseResult<Void> upCourse(MultipartFile imgFile, String code, String type,
                                         String id, String account, String introduce,
                                         Integer study, String isShow, String startTime, String endTime,
                                         String publisher, String title) {

        System.err.println(id);
        Course course = new Course();
        course.setId(id);
        course.setToken(account);
        course.setIntroduce(introduce);
        course.setStudy(study);
        course.setIsShow(isShow);
        course.setStartTime(startTime);
        course.setEndTime(endTime);
        course.setPublisher(publisher);
        course.setTitle(title);
        System.err.println(course);
        backendCollegeService.upCourse(imgFile, code, type, course);
        return new ResponseResult<>(SUCCESS);
    }


}
